
package com.mengmeizi.adapter;

import com.mengmeizi.R;
import com.mengmeizi.bean.ImageBean;
import com.mengmeizi.bean.ViewHolder;
import com.mengmeizi.util.Const;
import com.mengmeizi.util.ImageManager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import java.util.ArrayList;

public class ManagerImagesAdapter extends BaseAdapter {

    private Context context;

    // HashMap添加的时候会自动改变元素的位置，而LinkedHashMap则可以很好的保存添加时候的位置
    // private HashMap<String, Bitmap> imagesMap;
    // private LinkedHashMap<String, Bitmap> imagesMap;

    ArrayList<String> imageList;

    private LayoutInflater inflater;

    private ImageManager imgManager;

    public ManagerImagesAdapter(Context context, ArrayList<String> imageList) {
        this.context = context;

        imgManager = ImageManager.getManger();

        this.imageList = imageList;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        if (imageList != null) {
            return imageList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (imageList != null) {
            return imageList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageBean imageBean = null;
        if (imageList != null) {

            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_manager_rly, null);
                viewHolder = new ViewHolder();
                viewHolder.imvImage = (ImageView) convertView.findViewById(R.id.imvImage);
                viewHolder.imvImage.setDrawingCacheEnabled(true);
                viewHolder.imvImage.setVisibility(View.GONE);
                viewHolder.imvSelectImage = (ImageView) convertView.findViewById(R.id.imvSelectImage);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            String localPath = imageList.get(position);

            if (localPath != null && !"".equals(localPath)) {
                imageBean = new ImageBean();
                imageBean.selImage = viewHolder.imvSelectImage;
                imageBean.urlString = localPath;

                imgManager.loadImage2ImageView(context, localPath, viewHolder.imvImage, Const.TYPE_MANAGER_IMAGE, null);

                viewHolder.imvImage.setTag(imageBean);
                // viewHolder.imvImage.setOnClickListener((OnClickListener)
                // context);

                viewHolder.imvSelectImage.setVisibility(View.GONE); // 主动把所有选择背景图设置为隐藏，在删除图片的操作时起作用
            }

            return convertView;
        }
        return null;
    }

}
